import provinces from 'china-division/dist/provinces.json';
import cities from 'china-division/dist/cities.json';
import areas from 'china-division/dist/areas.json';

import {fromJS} from 'immutable';


const cityOptions = {
    '110000': {
        'name': '北京市',
        'code': '110100'
    },
    '120000': {
        'name': '天津市',
        'code': '120100',
    },
    '500000': {
        'name': '重庆市',
        'code': '500100',
    },
    '310000': {
        'name': '上海',
        'code': '310100',
    }
}

areas.forEach((area) => {
    const matchCity = cities.filter(city => city.code === area.parent_code)[0];
    if (matchCity) {

        matchCity.children = matchCity.children || [];
        matchCity.children.push({
            label: area.name,
            value: [area.code, area.name].join('-'),
        });
    }
});

cities.forEach((city) => {
    const matchProvince = provinces.filter(province => province.code === city.parent_code)[0];
    if (matchProvince && ['110000', '120000', '500000', '310000'].indexOf(city.parent_code) != -1) {
        matchProvince.children = matchProvince.children || [];
        matchProvince.children = [{
            label: cityOptions[city.parent_code].name,
            value: [cityOptions[city.parent_code].code, cityOptions[city.parent_code].name].join('-'),
            children: (!!matchProvince.children[0] ? matchProvince.children[0].children : []).concat(city.children),
        }];
    } else if (matchProvince) {
        matchProvince.children = matchProvince.children || [];
        matchProvince.children.push({
            label: city.name,
            value: [city.code, city.name].join('-'),
            children: city.children,
        });
    }
});

const options = provinces.map(province => ({
    label: province.name,
    value: [province.code, province.name].join('-'),
    children: province.children,
}));

export default options;